/**
 * 
 */
package com.zq.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *说明:
 *@version v1.0
 * @author zhangqian(mailto:739428054@qq.com)
 *	创建于:2020年4月25日下午6:40:40
 */
public class StreamUtil2 {
	
	/**
	* 功能：批量关闭流。需要Java1.7以上版本
	*
	* @param AutoCloseable... closeables 多个可关闭的对象
	*/
	public static void closings(AutoCloseable... closeables){
		if (closeables!=null) {
			for (AutoCloseable closeable : closeables) {
				if (closeable!=null) {
					try {
						closeable.close();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static String readText(InputStream src){
		BufferedReader reader=new BufferedReader(new InputStreamReader(src));
		String str="";
		StringBuffer buffer=new StringBuffer();
		try {
			while((str=reader.readLine())!=null){
				buffer.append(str);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			closings(reader);
		}
		
		return buffer.toString();
	}
	
	public static String readText(File src){
		String text ="";
		try {
			text = readText(new FileInputStream(src));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return text;
	}
	public static List<String> readTextToList(File file){
		List<String> list = new ArrayList<String>();
		try {
			BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			String str="";
			while((str=reader.readLine())!=null){
				list.add(str);
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
		
	}

}
